angular.module('newInvoice', []).controller('newInvoiceCtrl', [
    '$scope',
    'httpService',
    'alertmsg',
    '$state',
    function($scope, httpService, alertmsg, $state) {
        /* 获取可以开发票的订单 start */
        function getMyInvoiceOrders() {
            httpService.query('web/users/' + user.id + '/fetch_invoice_orders').then(function(res) {
                if (res.code == 200) {
                    $scope.orders = res.order
                } else {
                    alertmsg(res.msg);
                }
            });
        }
        getMyInvoiceOrders();
        /* 获取可以开发票的订单 end */

        // 所有选中的订单id
        var ids = [];
        /* 全选，反选 start */
        $scope.selectAll = false;
        $scope.all = function(state) {
            angular.forEach($scope.orders, function(v, k) {
                v.state = state;
                if(state) {
                    ids.push(v.id);
                } else {
                    ids = [];
                }
            });
        };
        /* 全选，反选 end */

        /* 查询选中的订单 start */
        $scope.getSeletedArr = function(index, state, id) {
            if(state) {
                ids.push(id);
            } else {
                ids.splice(index, 1);
            }
        };
        /* 查询选中的订单 end */

        /* 计算订单总价 start */
        $scope.orderTotal = function(total, measure_amount) {
            return parseFloat(total) + measure_amount;
        };
        /* 计算订单总价 end */

        /* 获取开票信息的默认数据 start */
        $scope.invoice = {
            invoice_type: 'common'
        };
        function getUserInvoiceInfo() {
            httpService.query('web/users/' + user.id + '/user_invoices').then(function(res) {
                if (res.code == 200) {
                    $scope.userInvoiceInfo = res.user_invoices[0];
                    if ($scope.userInvoiceInfo) {
                        $scope.invoice = $scope.userInvoiceInfo;
                    }
                } else {
                    alertmsg(res.msg);
                }
            });
        }
        getUserInvoiceInfo()
        /* 获取开票信息的默认数据 end */

        /* 新建发票 start */
        $scope.newInvoice = function() {
            addUserInvoice();
        };
        /* 新建发票 end */

        // 发票信息
        function addUserInvoice() {
            httpService.query('web/users/' + user.id + '/add_user_invoice', $scope.invoice, 'post').then(function(res) {
                if (res.code == 200) {
                    $scope.invoice = $scope.userInvoiceInfo = res.user_invoice;
                    $scope.invoice.user_invoice_id = $scope.userInvoiceInfo.id
                    addInvoice();
                } else {
                    alertmsg(res.msg);
                }
            });
        }

        // 开发票
        function addInvoice() {
            $scope.invoice.order_id = ids;
            httpService.query('web/users/' + user.id + '/add_invoice', $scope.invoice, 'post').then(function(res) {
                if (res.code == 200) {
                    $state.go("mine.invoiceDetail", {id: res.invoice.id});
                } else {
                    alertmsg(res.msg);
                }
            });
        }
    }
]);
